import 'package:bot_toast/bot_toast.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter/material.dart';
import 'package:lib_common/components/widget/ntt_auto_size_text.dart';

import '../components/dialog/ntt_loading_dialog.dart';
import '../r.dart';

/// toast 工具类
/// 使用前必须要初始化
/// 警告:不要随意调整调用BotToastInit函数的位置
/// final botToastBuilder = BotToastInit();  //1.调用BotToastInit
/// MaterialApp(
///       title: 'BotToast Demo',
///       builder: (context, child) {
///         child = myBuilder(context,child);  //do something
///         child = botToastBuilder(context,child);
///         return child;
///       },
///       navigatorObservers: [BotToastNavigatorObserver()], //2.注册路由观察者
///       home: XxxxPage(),
///   )
/// 或
/// MaterialApp(
///       title: 'BotToast Demo',
///       builder: BotToastInit(), //1.调用BotToastInit
///       navigatorObservers: [BotToastNavigatorObserver()], //2.注册路由观察者
///       home: XxxxPage(),
///   )
class NttToastUtils {
  static NttToastUtils? _instance;

  static NttToastUtils get instance {
    _instance ??= NttToastUtils._();
    return _instance!;
  }

  NttToastUtils._();

  /// 显示toast
  /// 如果字体显示异常 可能没有初始化 flutter_screenutil
  /// [text] 需要显示的文本
  /// [duration] 显示时长
  /// [contentColor] 内容背景颜色
  /// [borderRadius] 圆角
  /// [align] 设置显示区域偏移量 居中为 Alignment(0, 0)
  /// [onlyOne] 是否同一时间内只能显示一个，就是说弹出第二个时会将第一个消失掉
  /// [backButtonBehavior] 点击物理返回键的行为，[BackButtonBehavior.ignore] = 拦截返回事件，
  /// [BackButtonBehavior.close] = 关闭该toast,并且拦截事件停止冒泡，如果为null or [BackButtonBehavior.none]则不拦截事件
  static CancelFunc showToast(
    String text, {
    Duration? duration,
    Color? contentColor,
    TextStyle? textStyle,
    BorderRadius? borderRadius,
    Alignment? align,
    bool onlyOne = true,
  }) {
    return BotToast.showText(
        text: text,
        duration: duration ?? const Duration(seconds: 2),
        contentColor: contentColor ?? Colors.black.withOpacity(0.7),
        textStyle: textStyle ?? TextStyle(color: Colors.white, fontSize: 28.sp),
        borderRadius: borderRadius ?? BorderRadius.circular(16.r),
        align: align ?? const Alignment(0, 0.8),
        onlyOne: onlyOne);
  }

  /// 显示带错误图片的toast
  /// [text] 下面的描述文本
  /// [contentColor] 内容背景颜色
  /// [onlyOne] 是否同一时间内只能显示一个，就是说弹出第二个时会将第一个消失掉
  /// [borderRadius] 圆角
  /// [align] 设置显示区域偏移量 居中为 Alignment(0, 0)
  /// [imagePath] 需要显示的 icon 路径
  /// [maxWidth] 容器最大宽度
  /// [maxHeight] 容器最大高度
  /// [maxLines] 最大行数
  static CancelFunc showErrorToast(
    String text, {
    Color? contentColor,
    BorderRadius? borderRadius,
    Alignment? align,
    bool onlyOne = true,
    String imagePath = R.icon_toast_1_png,
    double? maxWidth,
    double? maxHeight,
    int? maxLines,
  }) {
    return BotToast.showCustomText(
      toastBuilder: (CancelFunc f) {
        return Container(
          constraints: BoxConstraints(
            minWidth: 120.w,
            minHeight: 120.w,
            maxWidth: maxWidth ?? 400.w,
            maxHeight: maxHeight ?? 400.w,
          ),
          padding:
              EdgeInsets.only(top: 28.w, bottom: 18.w, left: 18.w, right: 18.w),
          decoration: BoxDecoration(
            color: contentColor ?? Colors.black.withOpacity(0.7),
            borderRadius: borderRadius ?? BorderRadius.circular(16.w),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Image.asset(
                imagePath,
                width: 36.w,
                fit: BoxFit.contain,
              ),
              SizedBox(height: 10.w),
              NttAutoSizeText(
                text,
                style: TextStyle(color: Colors.white, fontSize: 13.sp),
                maxLines: maxLines ?? 2,
              ),
            ],
          ),
        );
      },
      align: align ?? const Alignment(0, 0),
      onlyOne: onlyOne,
    );
  }

  /// 显示默认loading
  /// [backgroundColor] 背景颜色
  /// [align] 设置显示区域偏移量 居中为 Alignment(0, 0)
  /// [backButtonBehavior] 点击物理返回键的行为，[BackButtonBehavior.ignore] = 拦截返回事件，
  /// [BackButtonBehavior.close] = 关闭该toast,并且拦截事件停止冒泡，如果为null or [BackButtonBehavior.none]则不拦截事件
  static CancelFunc showDefaultLoading({
    Color? backgroundColor,
    Alignment? align,
    BackButtonBehavior? backButtonBehavior = BackButtonBehavior.close,
  }) {
    return BotToast.showLoading(
      backgroundColor: backgroundColor ?? Colors.black.withOpacity(0.7),
      backButtonBehavior: backButtonBehavior,
      align: align ?? const Alignment(0, 0),
    );
  }

  /// 隐藏所有 loading
  static hideAllLoading() {
    BotToast.closeAllLoading();
  }

  /// 显示loading
  /// [backgroundColor] 背景颜色
  /// [backButtonBehavior] 点击物理返回键的行为，[BackButtonBehavior.ignore] = 拦截返回事件，
  /// [BackButtonBehavior.close] = 关闭该toast,并且拦截事件停止冒泡，如果为null or [BackButtonBehavior.none]则不拦截事件
  /// [lottiePath] lottie动画路径
  /// [width] 宽
  /// [height] 高
  /// [message] 描述文本
  static CancelFunc showLoading({
    Color? backgroundColor,
    BackButtonBehavior? backButtonBehavior,
    String? lottiePath,
    double? width,
    double? height,
    String? message,
  }) {
    return BotToast.showCustomLoading(
      backgroundColor:
          backgroundColor ?? const Color(0xff495166).withOpacity(0.7),
      backButtonBehavior: backButtonBehavior ?? BackButtonBehavior.close,
      toastBuilder: (cancelFunc) {
        return NttLoadingDialog(
          lottiePath: lottiePath ?? "packages/lib_common/assets/loading.json",
          width: width,
          height: height,
          message: message ?? "加载中…",
        );
      },
    );
  }
}
